package com.example.redis_spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class MyRedisController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/testString")
    @ResponseBody
    public String testString(){
        stringRedisTemplate.opsForValue().set("key1", "111");
        stringRedisTemplate.opsForValue().set("key2", "222");
        stringRedisTemplate.opsForValue().set("key3", "333");
        String value = stringRedisTemplate.opsForValue().get("key1");
        System.out.println(value);
        return "ok";
    }

    @GetMapping("/testList")
    @ResponseBody
    public String testList(){
        //清除之前的数据
        stringRedisTemplate.execute((RedisConnection connection) -> {
            //execute 要求回调方法中必须写return语句来返回一个东西
            //这个回调返回的对象就会作为execute本身的返回值。
            connection.flushAll();
            return null;
        });
        stringRedisTemplate.opsForList().leftPush("key1", "111");
        stringRedisTemplate.opsForList().leftPush("key1", "222");
        stringRedisTemplate.opsForList().leftPush("key1", "333");
        String value = stringRedisTemplate.opsForList().leftPop("key1");
        System.out.println(value);
        value = stringRedisTemplate.opsForList().leftPop("key1");
        System.out.println(value);
        value = stringRedisTemplate.opsForList().leftPop("key1");
        System.out.println(value);
        return "ok";
    }

    @GetMapping("/testSet")
    @ResponseBody
    public String testSet(){
        stringRedisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        stringRedisTemplate.opsForSet().add("key1", "111", "222", "333");
        Set<String> set = stringRedisTemplate.opsForSet().members("key1");
        System.out.println(set);
        boolean exists = stringRedisTemplate.opsForSet().isMember("key1", "111");
        System.out.println(exists);
        long size = stringRedisTemplate.opsForSet().size("key1");
        System.out.println(size);
        stringRedisTemplate.opsForSet().remove("key1", "111", "222");
        String result = stringRedisTemplate.opsForSet().pop("key1");
        System.out.println(result);
        set = stringRedisTemplate.opsForSet().members("key1");
        System.out.println(set);
        return "ok";
    }

    @GetMapping("/testHash")
    @ResponseBody
    public String testHash(){
        stringRedisTemplate.execute((RedisConnection connection) ->{
            connection.flushAll();
            return null;
        });
        stringRedisTemplate.opsForHash().put("key1", "f1", "111");
        stringRedisTemplate.opsForHash().put("key1", "f2", "222");
        stringRedisTemplate.opsForHash().put("key1", "f3", "333");
        long size = stringRedisTemplate.opsForHash().size("key1");
        System.out.println(size);
        String result = (String) stringRedisTemplate.opsForHash().get("key1", "f1");
        System.out.println(result);
        Boolean b = stringRedisTemplate.opsForHash().hasKey("key1", "f2");
        System.out.println(b);
        stringRedisTemplate.opsForHash().delete("key1", "f1", "f2");
        size = stringRedisTemplate.opsForHash().size("key1");
        System.out.println(size);
        return "ok";
    }

    @GetMapping("/testZSet")
    @ResponseBody
    public String testZset(){
        stringRedisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        stringRedisTemplate.opsForZSet().add("key1", "zhangsan", 16);
        stringRedisTemplate.opsForZSet().add("key1", "lishi", 15);
        stringRedisTemplate.opsForZSet().add("key1", "wangwu", 13);
        long size = stringRedisTemplate.opsForZSet().size("key1");
        System.out.println(size);
        Set<String> members = stringRedisTemplate.opsForZSet().range("key1", 0, -1);
        System.out.println(members);
        Set<ZSetOperations.TypedTuple<String>> membersWithScores = stringRedisTemplate.opsForZSet().rangeWithScores("key1", 0, -1);
        System.out.println("membersWithScores: " + membersWithScores);
        Long result = stringRedisTemplate.opsForZSet().rank("key1", "zhangsan");
        System.out.println(result);

        return "ok";
    }
}
